package com.zuimei.zuimei.activities;

import android.graphics.Color;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.view.KeyEvent;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.jeremyfeinstein.slidingmenu.lib.SlidingMenu;
import com.zuimei.zuimei.R;
import com.zuimei.zuimei.base.BaseActivity;
import com.zuimei.zuimei.fragments.CategoryFragment;
import com.zuimei.zuimei.fragments.EverydayFragment;
import com.zuimei.zuimei.fragments.FindAppFragment;
import com.zuimei.zuimei.fragments.GameRecommend;
import com.zuimei.zuimei.utils.SlidingMenuUtil;
import com.zuimei.zuimei.utils.ToastUtil;

import butterknife.Bind;

/**
 * 发现 应用页
 */

public class HomeActivity extends BaseActivity implements View.OnClickListener {
    @Bind(R.id.fl_main)
    FrameLayout fl_main;
    private FragmentManager mFragmentManager;
    private Fragment mShowFragment;
    private FragmentTransaction mTransaction;
    private TextView tv_find_app;
    private TextView tv_everyday;
    private TextView tv_category;
    private TextView tv_game_recommend;
    private TextView tv_app_panking;
    private LinearLayout ll_login;
    private SlidingMenu mMenu;

    @Override
    public int getLayoutId() {
        return R.layout.activity_home;
    }

    @Override
    protected void init() {

        SlidingMenuUtil util = SlidingMenuUtil.getInstance();
        util.initSlidingMenu(this);
        mMenu = util.getSlideMenu(this);
        View view = mMenu.getMenu();
        tv_find_app = (TextView) view.findViewById(R.id.tv_find_app);
        tv_everyday = (TextView) view.findViewById(R.id.tv_everyday);
        tv_category = (TextView) view.findViewById(R.id.tv_category);
        tv_game_recommend = (TextView) view.findViewById(R.id.tv_game_recommend);
        tv_app_panking = (TextView) view.findViewById(R.id.tv_app_panking);
        ll_login = (LinearLayout) view.findViewById(R.id.ll_login);
        tv_find_app.setOnClickListener(this);
        tv_everyday.setOnClickListener(this);
        tv_category.setOnClickListener(this);
        tv_game_recommend.setOnClickListener(this);
        tv_app_panking.setOnClickListener(this);
        ll_login.setOnClickListener(this);
        mFragmentManager = getSupportFragmentManager();
        mShowFragment = new FindAppFragment();
        mFragmentManager
                .beginTransaction()
                .add(R.id.fl_main, mShowFragment, mShowFragment.getClass().getName())
                .commit();

    }

    @Override
    public void onClick(View view) {
        mFragmentManager = getSupportFragmentManager();
        switch (view.getId()) {
            case R.id.tv_find_app:
                mMenu.getMenu().setBackgroundColor(Color.parseColor("#009FD3"));
                showFragment(new FindAppFragment());
                break;
            case R.id.tv_everyday:
                mMenu.getMenu().setBackgroundColor(Color.parseColor("#8f2ab6"));
                showFragment(new EverydayFragment());
                break;
            case R.id.tv_category:
                mMenu.getMenu().setBackgroundColor(Color.parseColor("#009FD3"));
                showFragment(new CategoryFragment());
                break;
            case R.id.tv_game_recommend:
                mMenu.getMenu().setBackgroundColor(Color.parseColor("#009FD3"));
                showFragment(new GameRecommend());
                break;
            case R.id.tv_app_panking:
                mMenu.getMenu().setBackgroundColor(Color.parseColor("#009FD3"));
                break;
            case R.id.ll_login:
                //登录
                break;
        }
        mMenu.toggle();
    }

    /**
     * 显示fragment
     */
    private void showFragment(Fragment fragment) {
        mFragmentManager = getSupportFragmentManager();
        mTransaction = mFragmentManager.beginTransaction();
        if (mShowFragment != null) {//获取显示的fragment，如果有，则隐藏
            mTransaction.hide(mShowFragment);
        }
        //通过传进来的fragment的类名作为tag查看fragment是否存在
        mShowFragment = mFragmentManager.findFragmentByTag(fragment.getClass().getName());
        if (mShowFragment != null) {
            //存在则显示
            mTransaction.show(mShowFragment);
        } else {
            //不存在则添加一个
            mTransaction.add(R.id.fl_main, fragment, fragment.getClass().getName());
            mShowFragment = fragment;//并将当前显示frament设为添加的fragment
        }
        mTransaction.commit();
    }

    int flag = 0;

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        switch (keyCode) {
            case KeyEvent.KEYCODE_BACK:
                flag++;
                if (!mMenu.isMenuShowing()) {
                    mMenu.toggle();
                    return false;
                } else {
                    exit();
                    return false;
                }
        }
        return super.onKeyDown(keyCode, event);
    }

    private long exitTime = 0;

    private void exit() {
        if ((System.currentTimeMillis() - exitTime) > 2000) {
            ToastUtil.showToast(this, "再按一次退出");
            exitTime = System.currentTimeMillis();
        } else {
            finish();
            System.exit(0);
        }
    }
}
